#include "../common/dispatcher.hpp"
#include "../common/message.hpp"
#include "../common/detail.hpp"
#include "../common/net.hpp"
#include "../client/requestor.hpp"
#include "../client/rpc_caller.hpp"
#include <chrono>
#include <jsoncpp/json/value.h>
#include <memory>
#include <thread>

// void onRpcResponse(const rpc::BaseConnection::ptr& conn, rpc::RPCResponse::ptr& msg)
// {
//     std::cout<<"收到了Rpc响应：";
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;
// }

// int main()
// {
//     auto dispatcher = std::make_shared<rpc::DisPatcher>();
//     dispatcher->registerHandler<rpc::RPCResponse>(rpc::MType::RSP_RPC, onRpcResponse);
    

//     auto client = rpc::ClientFactory::create("127.0.0.1",9090);
//     auto message_cb = std::bind(&rpc::DisPatcher::onMessage, dispatcher.get(), std::placeholders::_1,std::placeholders::_2);
//     client->setMessageCallback(message_cb);
//     client->connect();
//     auto rpc_req = rpc::MessageFactory::create<rpc::RpcRequest>();
//     rpc_req->setMethod("Add");
//     rpc_req->setId("1111");
//     Json::Value params;
//     params["num1"] = 11;
//     params["num2"] = 22;
//     rpc_req->setParams(params);
//     rpc_req->setMtype(rpc::MType::REQ_RPC);
//     client->send(rpc_req);
//     sleep(10);
//     client->shutdown();
//     return 0;
// }




// void callback(const Json::Value &result)
// {
//     std::cout<< "result: " << result.asInt() << std::endl;
// }

// int main()
// {
//     auto requestor = std::make_shared<rpc::client::Requestor>();
//     auto caller =  std::make_shared<rpc::client::RpcCaller>(requestor);

//     auto dispatcher = std::make_shared<rpc::DisPatcher>();
//     auto rsp_cb = std::bind(&rpc::client::Requestor::onResponse, requestor.get(), std::placeholders::_1, std::placeholders::_2);
//     dispatcher->registerHandler<rpc::BaseMessage>(rpc::MType::RSP_RPC, rsp_cb);

//     auto client = rpc::ClientFactory::create("127.0.0.1", 9090);
//     auto message_cb = std::bind(&rpc::DisPatcher::onMessage, dispatcher.get(), std::placeholders::_1, std::placeholders::_2);
//     client->setMessageCallback(message_cb);
//     client->connect();

//     auto conn = client->connection();
//     Json::Value params, result;
//     params["num1"] = 11;
//     params["num2"] = 22;
//     bool ret = caller->call(conn, "Add", params, result);
//     if(ret)
//     {
//         std::cout<<"result:" << result.asInt() <<std::endl;
//     }

//     params["num1"] = 33;
//     params["num2"] = 55;
//     rpc::client::RpcCaller::JsonAsyncResponse res_future;
//     ret = caller->call(conn, "Add", params, res_future);
//     if(ret)
//     {
//         result = res_future.get();
//         std::cout<< "result:" << result.asInt() << std::endl;
//     }

//     params["num1"] = 66;
//     params["num2"] = 77;
//     caller->call(conn, "Add", params, callback);

//     //rpc::DLOG("--------\n");
//     std::this_thread::sleep_for(std::chrono::seconds(1));

//     client->shutdown();
//     return 0;
// }

#include "../client/rpc_client.hpp"
#include "../common/detail.hpp"
#include <thread>
using namespace rpc;

void callback(const Json::Value &result)
{
    std::cout<< "result: " << result.asInt() << std::endl;
}

int main()
{

    rpc::client::RpcClient client(false, "127.0.0.1", 9090);

    Json::Value params, result;
    params["num1"] = 11;
    params["num2"] = 22;
    bool ret = client.call("Add", params, result);
    if(ret)
    {
        std::cout<<"result:" << result.asInt() <<std::endl;
    }

    params["num1"] = 33;
    params["num2"] = 55;
    rpc::client::RpcCaller::JsonAsyncResponse res_future;
    ret = client.call("Add", params, res_future);
    if(ret)
    {
        result = res_future.get();
        std::cout<< "result:" << result.asInt() << std::endl;
    }

    params["num1"] = 66;
    params["num2"] = 77;
    client.call("Add", params, callback);

    DLOG("--------\n");
    std::this_thread::sleep_for(std::chrono::seconds(2));

    return 0;
}